package com.seehope.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.seehope.po.User;
import com.seehope.service.UserService;
import com.seehope.service.impl.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name = "LoginServlet", urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private UserService userService =new UserServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username=request.getParameter("username");
		String password =request.getParameter("password");
		if(!"".equals(username) && null!=username && !"".equals(password) && null!=password){
	           User	user=userService.login(username, password);
	           if(user!=null){
	        	   request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp").forward(request, response);
	           }else{
	        	   request.setAttribute("message", "用户名或者密码错误");
	   			   request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);  
	           }
		}else{
			request.setAttribute("message", "用户名或者密码不允许为空");
			request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		}
	}

}
